package com.swgk.core.view.weiget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.swgk.core.R;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.interfaces.PopClick;
import com.swgk.core.interfaces.PopName;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.adapter.TwoLevelAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : younghow
 * @date : 2021/12/8 10:42
 * description:
 */
public class TwoLevelPopWindow<T extends PopName> extends BasePopWindow implements View.OnClickListener {

    private RecyclerView mRlProvince;
    private RecyclerView mRlCity;
    private TextView mTvReSet;
    private TextView mTvOk;
    private LinearLayoutManager mProvinceManager;
    private LinearLayoutManager mCityManager;
    private TwoLevelAdapter<T> mProvinceAdapter;
    private TwoLevelAdapter<T> mCityAdapter;
    private PopClick<T> mPopClick;
    private boolean isLink = true;
    private int provincePosition;
    private int cityPosition;
    private List<List<T>> mLinkList;
    private T provinceEntity,cityEntity;

    public void setPopClick(PopClick<T> mPopClick) {
        this.mPopClick = mPopClick;
    }

    public TwoLevelPopWindow(Context context) {
        super(context);
    }

    public TwoLevelPopWindow(Context context, int dir) {
        super(context, dir);
    }

    @Override
    protected int popLayout() {
        return R.layout.pop_window_two_level;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mRlProvince = holder.getPop().findViewById(R.id.rl_province);
        mRlCity = holder.getPop().findViewById(R.id.rl_city);

        mTvReSet = holder.getPop().findViewById(R.id.tv_re_set);
        mTvOk = holder.getPop().findViewById(R.id.tv_ok);

        mProvinceManager = new LinearLayoutManager(mContext);
        mCityManager = new LinearLayoutManager(mContext);

        mProvinceAdapter = new TwoLevelAdapter<>(mContext, null, R.layout.item_base_province);
        mCityAdapter = new TwoLevelAdapter<>(mContext, null, R.layout.item_base_province);
        mProvinceAdapter.setItemSelect(true);
        mCityAdapter.setItemSelect(true);
        mCityAdapter.setImgStyle();
        mProvinceAdapter.setOnItemClick(new AdapterOnItemClick<T>() {
            @Override
            public void onItemClick(T t, int position) {
                if (position == 0){
                    cityEntity = null;
                }
                boolean flag = false;
                if (provincePosition != position){
                    flag = true;
                }

                provincePosition = position;
                provinceEntity = t;
                if (position != 0){
                    if (isLink){
                        mCityAdapter.setDatas(mLinkList.get(position));
                        if (flag){
                            cityEntity = null;
                            mCityAdapter.setDefItem(0);
                        }
                    }
                }else {
                    provinceEntity = null;
                    mCityAdapter.setDatas(new ArrayList<>());
                }
            }
        });

        mCityAdapter.setOnItemClick(new AdapterOnItemClick<T>() {
            @Override
            public void onItemClick(T t, int position) {
                if (provincePosition == 0){
                    cityEntity = null;
                    return;
                }
                cityEntity = t;
                cityPosition = position;
            }
        });

        mRlProvince.setAdapter(mProvinceAdapter);
        mRlCity.setAdapter(mCityAdapter);

        mRlProvince.setLayoutManager(mProvinceManager);
        mRlCity.setLayoutManager(mCityManager);
        mProvinceAdapter.setDefItem(0);

        mTvReSet.setOnClickListener(this);
        mTvOk.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_re_set) {
            setProvinceItem(0);
            mCityAdapter.setDatas(new ArrayList<>());
            provinceEntity = null;
            cityEntity = null;
            mPopClick.reset();
            dismiss();
        }else if (v.getId() == R.id.tv_ok) {
            mPopClick.sure(provincePosition,cityPosition,provinceEntity,cityEntity);
            dismiss();
        }
    }

    public void setLinkData(List<T> province,List<List<T>> city){
        this.mLinkList = city;
        mProvinceAdapter.setDatas(province);
        isLink = true;
    }

    public void setLinkData(List<T> province,List<List<T>> city,String oneLevelNo,String twoLevelNo){
        this.mLinkList = city;
        mProvinceAdapter.setDatas(province);
        int selectProvincePosition = 0;
        for (int i = 0; i < province.size(); i++) {
            if (province.get(i).popNo().equals(oneLevelNo)){
                mProvinceAdapter.setDefItem(i);
                selectProvincePosition = i;
                provincePosition = i;
                provinceEntity = province.get(i);
            }
        }

        if (city.get(selectProvincePosition) == null || city.get(selectProvincePosition).size() <= 0){
            mCityAdapter.setDatas(new ArrayList<>());
        }else {
            mCityAdapter.setDatas(city.get(selectProvincePosition));
        }
        if (city.size() > 0){
            for (int i = 0; i < city.get(selectProvincePosition).size(); i++) {
                if (city.get(selectProvincePosition).get(i).popNo().equals(twoLevelNo)){
                    mCityAdapter.setDefItem(i);
                }
            }
        }
        isLink = true;
    }

    public void setData(List<T> province,List<T> city){
        this.mLinkList = new ArrayList<>();
        isLink = false;
        mProvinceAdapter.setDatas(province);
        mCityAdapter.setDatas(city);
    }

    public void setProvinceItem(int position){
        mProvinceAdapter.setDefItem(position);
    }

    public void setCityItem(int position){
        mCityAdapter.setDefItem(position);
    }
}
